<template>
  <el-card class="stats-card">
    <template #header>
      <div class="card-header">
        <span>评论统计</span>
        <el-button @click="refreshStats" :loading="loading">
          <el-icon><Refresh /></el-icon>
          刷新
        </el-button>
      </div>
    </template>
    
    <div class="stats-content">
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="stat-item">
            <div class="stat-number">{{ stats.totalCount || 0 }}</div>
            <div class="stat-label">总评论数</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-item">
            <div class="stat-number">{{ stats.todayCount || 0 }}</div>
            <div class="stat-label">今日评论</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-item">
            <div class="stat-number">{{ stats.weekCount || 0 }}</div>
            <div class="stat-label">本周评论</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-item">
            <div class="stat-number">{{ stats.monthCount || 0 }}</div>
            <div class="stat-label">本月评论</div>
          </div>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Refresh } from '@element-plus/icons-vue'
import { getCommentStats } from '@/api/comment'

const loading = ref(false)
const stats = ref({})

// 获取统计数据
const fetchStats = async () => {
  try {
    loading.value = true
    console.log('开始获取评论统计数据...')
    const result = await getCommentStats()
    console.log('统计数据响应:', result)
    if (result.code === 200) {
      stats.value = result.data
      console.log('统计数据获取成功:', stats.value)
    } else {
      console.warn('统计数据响应异常:', result)
      // 设置默认统计数据，不阻塞页面
      stats.value = {
        totalCount: 0,
        todayCount: 0,
        weekCount: 0,
        monthCount: 0
      }
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
    // 设置默认统计数据，不阻塞页面
    stats.value = {
      totalCount: 0,
      todayCount: 0,
      weekCount: 0,
      monthCount: 0
    }
    // 不显示错误消息，避免影响用户体验
  } finally {
    loading.value = false
  }
}

// 刷新统计
const refreshStats = () => {
  fetchStats()
}

// 组件挂载时获取数据
onMounted(() => {
  fetchStats()
})

// 暴露刷新方法给父组件
defineExpose({
  refreshStats
})
</script>

<style lang="scss" scoped>
.stats-card {
  margin-bottom: 20px;
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
    color: #303133;
  }
  
  .stats-content {
    .stat-item {
      text-align: center;
      padding: 20px;
      background: #f8f9fa;
      border-radius: 8px;
      transition: all 0.3s;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      }
      
      .stat-number {
        font-size: 28px;
        font-weight: bold;
        color: #409eff;
        margin-bottom: 8px;
      }
      
      .stat-label {
        font-size: 14px;
        color: #606266;
      }
    }
  }
}
</style>
